<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */
 
$cadena = dirname(__FILE__);
$fpos = strrpos($cadena, "/");
if ($fpos<=0) {
	$cadena = str_replace("\\","/",$cadena);
	$fpos = strrpos($cadena, "/");
}

define('ROOT_PATH',substr($cadena, 0, $fpos)."/");

require_once(ROOT_PATH."includes/session.php");
require_once(ROOT_PATH."func/sessioncache.php"); 
require_once(ROOT_PATH."includes/util.func.php");
require_once(ROOT_PATH."func/functions.inc.php");
require_once(ROOT_PATH."func/filefunctions.inc.php");
require_once(ROOT_PATH."includes/mail.inc.php");

require_once(ROOT_PATH."configs/config.php");
//require_once(ROOT_PATH."configs/lang.php");

$param=util::fillparam();

function __autoload($class_name) {
	$include_path = ROOT_PATH."class/";
	$path = $include_path . $class_name . '.class.php';
	if(file_exists($path)){
		require_once $path;
	}
}

$_log =& LoggerManager::getLogger('default');

$CONFFILE = ROOT_PATH."configs/dbconf.php";

$_log->debug("INIT SETUP");
$_log->debug($param);

if ($param['op']="save") {
	$dbuser = $param['dbusername'];
	$dbpass = $param['dbpassword'];
	$dbhost = $param['dbhost'];
	$dbname = $param['dbname'];

	$_log->debug("TEST CONNECT");
	$db = new dbmanager($dbhost,$dbuser,$dbpass);
	if (!$db->testConnect($dbname)) die($db->error);

	if (!isset($param['re'])) 	if (!($f = fopen($CONFFILE, 'w'))) die("Check file permissions");	
	
	$db->connect($dbname);
	$loader = new loader($db);
	
	$includeData=true; 
	include_once(ROOT_PATH."configs/dbdefines.php");	

	foreach ($table_def as $v) {
		if ($v['setup']!="no") {
			$q = String::format($v['def'],$v['name']);
			$loader->execute($q);
			foreach ($v['data'] as $d) {
				$q = String::format($d,$v['name']);
				$loader->execute($q);
			}
		}
	}

	if (!isset($param['re'])) {
		$filedata = "<?php\n";
		$filedata .= "define('DB_USER','".$dbuser."');\n";
		$filedata .= "define('DB_PASS','".$dbpass."');\n";
		$filedata .= "define('DB_HOST','".$dbhost."');\n";
		$filedata .= "define('DB_NAME','".$dbname."');\n";
		$filedata .= "?>";
					
		fwrite($f, $filedata);
		fclose($f);
	}
	
	$us = new user;
	$us->userid=$param['adminuser'];
	$us->mail=$param['adminmail'];
	$us->lang="en";
	$us->password=$param['adminpass'];
	$us->groupid=GROUP_ADMIN;		
	$us->status=STATUS_ACTIVE;
	$ret = $loader->save($us);

	$db->close();
	unset($loader);
	unset($db);		
	
	echo "Setup Complete!!";
	
}


?>
